//
//  XLConversationInputMoreItemView.m
//  XLConversationInputView
//
//  Created by ilongge on 2022/8/5.
//

#import "XLCvstInputMoreItemView.h"
#import "XLCvstInputMoreItemCell.h"

@interface XLCvstInputMoreItemView()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) UICollectionView *itemCollection;
@property (nonatomic, strong) NSArray <XLCvstInputMoreItemModel *> *supportMoreItems;
@end

@implementation XLCvstInputMoreItemView

- (instancetype)initWithFrame:(CGRect)frame 
             supportMoreItems:(NSArray <XLCvstInputMoreItemModel *> *)supportMoreItems
{
    self = [super initWithFrame:frame];
    if (self) {
        _supportMoreItems = supportMoreItems;
        [self initData];
        [self initSubviews];
    }
    return self;
}

- (void)initData {
    self.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
}


-(NSInteger)itemCounts {
    return self.supportMoreItems.count;
}

- (void)initSubviews {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
    view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    [self addSubview:view];
    [self.itemCollection registerClass:[XLCvstInputMoreItemCell class]
            forCellWithReuseIdentifier:@"XLConversationInputMoreItemCell"];
    [self addSubview:self.itemCollection];
    [self.itemCollection reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.itemCollection.frame = CGRectMake(35, 25, self.frame.size.width - 70, self.frame.size.height - 30);
}

- (void)clickAtItem:(XLCvstInputMoreItemModel *)model {
    if (self.clickItemHandler) {
        self.clickItemHandler(model);
    }
}

#pragma mark UICollectionViewDelegate / UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger count = self.supportMoreItems.count;
    return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XLCvstInputMoreItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XLConversationInputMoreItemCell"
                                                                              forIndexPath:indexPath];
    id itemModel = self.supportMoreItems[indexPath.row];
    [cell configMoreItemCellWith:itemModel];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    XLCvstInputMoreItemModel *itemModel = self.supportMoreItems[indexPath.row];
    [self clickAtItem:itemModel];
}
#pragma mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(ItemWidth, ItemHeight);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return ItemLineMargin;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return self.minimumInteritemSpacing;
}

- (UICollectionView *)itemCollection {
    if (_itemCollection == nil) {
        UICollectionViewFlowLayout *collectionLayOut = [[UICollectionViewFlowLayout alloc] init];
        collectionLayOut.minimumInteritemSpacing = 0.1;
        collectionLayOut.itemSize = CGSizeMake(200, 100);
        collectionLayOut.minimumLineSpacing = 8;
        collectionLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        _itemCollection = [[UICollectionView alloc] initWithFrame:CGRectZero
                                             collectionViewLayout:collectionLayOut];
        _itemCollection.delegate = self;
        _itemCollection.dataSource = self;
        _itemCollection.pagingEnabled = YES;
        _itemCollection.backgroundColor = [UIColor clearColor];
    }
    return _itemCollection;
}
@end
